"""有限状态机

状态
合法状态
结束状态
状态转移表

当前状态
状态转移循环
终止条件

"""

class Solution:
    def isNumber(self, s: str) -> bool:
        """时间复杂度O(N),空间复杂度O(1)

        Args:
            s (str): _description_

        Returns:
            bool: _description_
        """
        states = [
            {' ': 0, 's': 1, 'd': 2, '.': 3},
            {'d': 2, '.': 3},
            {'d': 2, '.': 4, 'e': 5, ' ': 8},
            {'d': 4},
            {'d': 4, 'e': 5, ' ': 8},
            {'s': 6, 'd': 7},
            {'d': 7},
            {'d': 7, ' ': 8},
            {' ': 8}
        ]
        p = 0
        for c in s:
            if '0' <= c <= '9':
                t = 'd'
            elif c in "+-":
                t = 's'
            elif c in "eE":
                t = 'e'
            elif c in ". ":
                t = c
            else:
                t = '?'
            if t not in states[p]:
                return False
            p = states[p][t]
        return p in (2,4,7,8)
